package com.zjmzxfzhl.modules.flowable.controller;

import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.util.ObjectUtils;
import com.zjmzxfzhl.modules.flowable.common.BaseFlowableController;
import com.zjmzxfzhl.modules.flowable.service.WorkflowService;
import com.zjmzxfzhl.modules.flowable.vo.TaskRequest;
import com.zjmzxfzhl.modules.flowable.vo.WorkflowVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 艾迪
 * @date 2021年7月23日
 */
@Api(tags = "工作流")
@RestController
@RequestMapping("/flowable/workflow")
public class WorkflowController extends BaseFlowableController {
    @Autowired
    private WorkflowService workflowService;

    @ApiOperation(value = "完成任务")
    @PutMapping(value = "/complete")
    public Result complete(@RequestBody TaskRequest taskRequest) {
        WorkflowVo vo = new WorkflowVo();
        if (StringUtils.isEmpty(taskRequest.getProcessInstanceId())) {
            return Result.error("processInstanceId cannot be null");
        }
        vo.setProcessInstanceId(taskRequest.getProcessInstanceId());
        vo.setTaskName(taskRequest.getTaskName());
        vo.setTaskDefinitionKey(taskRequest.getTaskDefinitionKey());
        WorkflowVo resultVo = workflowService.findTaskByProcessInstanceId(vo);
        if (ObjectUtils.isEmpty(resultVo)) {
            return Result.error("cannot find valid task");
        }
        taskRequest.setTaskId(resultVo.getTaskId());
        flowableTaskService.completeTask(taskRequest);
        return Result.ok();
    }

    @ApiOperation(value = "查询task信息")
    @PostMapping(value = "/findTaskInfo")
    public Result findTaskByProcessInstanceId(@RequestBody WorkflowVo workflowVo) {
        WorkflowVo resultVo = workflowService.findTaskByProcessInstanceId(workflowVo);
        return Result.ok(resultVo);
    }
    
}
